package org.db.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;

import javax.swing.table.AbstractTableModel;

import org.db.dao.HoehenlinienDAO;

public class HoehenlinienTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4142436180063456905L;

	private List<HoehenlinienDAO> data = new ArrayList<HoehenlinienDAO>();

	private DecimalFormat format1 = new DecimalFormat("0.000");

	private DecimalFormat format2 = new DecimalFormat("0");

	Class<?>[] types = new Class[]{
			//
			java.lang.Double.class, java.lang.Double.class,
			java.lang.Double.class, java.lang.Double.class,
			java.lang.Integer.class, java.lang.Double.class,
			java.lang.Double.class, java.lang.Double.class,
			java.lang.Double.class,};

	@Override
	public int getColumnCount() {
		return 9;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		switch (column) {
			case 0 :
				return data.get(row).getNeigungAn() == 0 ? "" : format1
						.format(data.get(row).getNeigungAn());
			case 1 :
				return data.get(row).getLaengeAn() == 0 ? "" : format1
						.format(data.get(row).getLaengeAn());
			case 2 :
				return data.get(row).gethoehe() == 0 ? "" : format1.format(data
						.get(row).gethoehe());
			case 3 :
				return data.get(row).getStationierungInMeter() == 0
						? ""
						: format1.format(data.get(row)
								.getStationierungInMeter());
			case 4 :
				return data.get(row).getRa() == 0 ? "" : format2.format(data
						.get(row).getRa());
			case 5 :
				return data.get(row).getLt() == 0 ? "" : format1.format(data
						.get(row).getLt());
			case 6 :
				return data.get(row).getA() == 0 ? "" : format1.format(data
						.get(row).getA());
			case 7 :
				return data.get(row).getNeigungAb() == 0 ? "" : format1
						.format(data.get(row).getNeigungAb());
			case 8 :
				return data.get(row).getLaengeAb() == 0 ? "" : format1
						.format(data.get(row).getLaengeAb());
			default :
				return "";
		}
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		HoehenlinienDAO dao = data.get(rowIndex);
		switch (columnIndex) {
			case 0 :
				dao.setNeigungAn((Double) value);
				break;
			case 1 :
				dao.setLaengeAn((Double) value);
				break;
			case 2 :
				dao.sethoehe((Double) value);
				break;
			case 3 :
				dao.setStationierungInMeter((Double) value);
				break;
			case 4 :
				dao.setRa((Integer) value);
				break;
			case 5 :
				dao.setLt((Double) value);
				break;
			case 6 :
				dao.setA((Double) value);
				break;
			case 7 :
				dao.setNeigungAb((Double) value);
				break;
			case 8 :
				dao.setLaengeAb((Double) value);
				break;
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public String getColumnName(int column) {
		switch (column) {
			case 0 :
				return "ank.Neigung";
			case 1 :
				return "ank.Länge";
			case 2 :
				return "Höhe";
			case 3 :
				return "Stationierung";
			case 4 :
				return "ra";
			case 5 :
				return "lt";
			case 6 :
				return "a";
			case 7 :
				return "abg.Neigung";
			case 8 :
				return "abg.Länge";
			default :
				return "";
		}
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public List<HoehenlinienDAO> getHoehenlinienElemente() {
		return data;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return types[columnIndex];
	}

	public void addHoehenlinienElement(HoehenlinienDAO dao) {
		data.add(dao);
		Collections.sort(data);
		fireTableDataChanged();
	}

	public HoehenlinienDAO getElementAtRow(int row) {
		if (row < data.size()) {
			return data.get(row);
		}
		return null;
	}

	public void addHoehenlinienElemente(SortedSet<HoehenlinienDAO> daos) {
		data.addAll(daos);
		fireTableDataChanged();
	}

	public void removeHoehenlinienElement(HoehenlinienDAO dao) {
		data.remove(dao);
		fireTableDataChanged();
	}

	public void replaceHoehenlinienElement(HoehenlinienDAO oldDao,
			HoehenlinienDAO newDao) {
		data.set(data.indexOf(oldDao), newDao);
		fireTableDataChanged();
	}

	public HoehenlinienDAO removeHoehenlinienElement(int rowIndex) {
		HoehenlinienDAO removedDAO = data.remove(rowIndex);
		fireTableDataChanged();
		return removedDAO;
	}

	public void refresh(HoehenlinienDAO dao) {
		int row = data.indexOf(dao);
		fireTableRowsUpdated(row, row);
	}

	public void refresh() {
		fireTableDataChanged();
	}

	public boolean isEmpty() {
		return data.isEmpty();
	}

}
